import path from 'path';
import { ConfigEnv, UserConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

// https://vitejs.dev/config/
export default ({ mode, command }: ConfigEnv): UserConfig => {
  if (command === 'build') {
    return {
      plugins: [],
      build: {
        lib: {
          entry: path.resolve(process.cwd(), 'src/index.ts'),
          name: 'lib',
          fileName: format => `yu-flow-vue.${format}.js`,
          formats: ['es', 'umd'],
        },
        outDir: path.resolve(process.cwd(), 'dist/vue'),
        rollupOptions: {
          external: ['vue'],
          output: {
            globals: {
              vue: 'Vue',
            },
          },
        },
      },
      esbuild: {
        jsxFactory: 'h',
        jsxFragment: 'Fragment',
        jsxInject: `import { h } from 'vue'`,
      },
    };
  }
  if (mode === 'vue') {
    return {
      plugins: [vue()],
      esbuild: {
        jsxFactory: 'h',
        jsxFragment: 'Fragment',
        jsxInject: `import { h } from 'vue'`,
      },
    };
  }
  return {};
};
